package com.itheima.service.front.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.front.MemberDao;
import com.itheima.domain.front.Member;
import com.itheima.factory.MapperFactory;
import com.itheima.service.front.MemberService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.UUIDUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

public class MemberServiceImpl implements MemberService {


    @Override
    public boolean register(Member member) {
        SqlSession sqlSession = null;
        //1.获取SqlSession
        sqlSession = MapperFactory.getSqlSession();
        //2.获取Dao
        MemberDao memberDao = sqlSession.getMapper(MemberDao.class);
        //id使用UUID的生成策略来获取
        String id = UUIDUtil.getSimpleUUID().toString();
        member.setId(id);
        member.setRegisterDate(new Date());
        member.setState("1");
        member.setPassword(MD5Util.md5(member.getPassword()));
        //3.调用Dao层操作
        int row = memberDao.add(member);
        //4.提交事务
        sqlSession.commit();
        return row > 0;
    }

    @Override
    public List<Member> findAll() throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        MemberDao memberdao = sqlSession.getMapper(MemberDao.class);
        List<Member> users = memberdao.findAll();

        sqlSession.close();
        return users;
    }

    @Override
    public Member findById(String memberId) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        MemberDao memberdao = sqlSession.getMapper(MemberDao.class);
        Member member = memberdao.findById(memberId);

        sqlSession.close();
        return member;
    }

    @Override
    public void add(Member member) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        MemberDao memberdao = sqlSession.getMapper(MemberDao.class);
        member.setId(UUIDUtil.getSimpleUUID());
        member.setPassword(MD5Util.md5(member.getPassword()));
        memberdao.add(member);

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteById(String... memberIds) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        MemberDao memberdao = sqlSession.getMapper(MemberDao.class);
        for (String memberId : memberIds) {
            memberdao.deleteById(memberId);
        }

        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void update(Member member) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        MemberDao memberdao = sqlSession.getMapper(MemberDao.class);
        member.setPassword(MD5Util.md5(member.getPassword()));
        memberdao.update(member);

        sqlSession.commit();
        sqlSession.close();
    }


    @Override
    public PageInfo queryPage(Integer pageNum, Integer pageSize) throws Exception {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        MemberDao memberdao = sqlSession.getMapper(MemberDao.class);
        PageHelper.startPage(pageNum, pageSize);
        List<Member> users = memberdao.findAll();

        PageInfo<Member> pageInfo = new PageInfo<>(users);
        sqlSession.close();
        return pageInfo;

    }


}
